package tcu.iot.gasmanage.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import tcu.iot.gasmanage.common.Result;
import tcu.iot.gasmanage.entity.user.User;
import tcu.iot.gasmanage.service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(value = "登录用户控制器")
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Resource
    private UserService userService;

    @ApiOperation(value = "/login", notes = "登录功能实现，生成token")
    @PostMapping("/login")
    public Result login(@RequestBody User RequestUser) {
        return userService.LoginUser(RequestUser);
    }

    @ApiOperation(value = "/info", notes = "解析token，返回登录信息")
    @GetMapping("/info")
    public Result loginInfo(HttpServletRequest request) {
        return userService.LoginInfo(request.getHeader("Authorization"));
    }

    @ApiOperation(value = "/logout", notes = "退出登录")
    @PostMapping("/logout")
    public Result logout(HttpServletRequest request) {
        String token = request.getHeader("Authorization");

        return userService.LoginOut(token);
    }

    @ApiOperation(value = "/update", notes = "修改密码")
    @PostMapping("/update")
    public Result update(@Param(value = "username") String username, @Param(value = "oldPassword") String oldPassword,
                         @Param(value = "newPassword") String newPassword) {
        return userService.UpdatePassword(username, oldPassword, newPassword);
    }
}
